
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using System.Linq;
using DG.Tweening;


namespace CardEffects
{
    public class CardEffect_AddShield : BaseCardEffectModule
    {
        // public int buff_id { get; set; } = 0;
        public int buff_id { get; set; } = 3;

        public int creator_card_uid { get; set; } = -1;
        
        public override void Apply(GameObject card, CardUseInfo useInfo = null)
        {
            var card_comp = card.GetComponent<CardController>();
            var buff_comp = card.GetComponent<BuffHandle>();
            
            if (card_comp.parameter.card_uid != creator_card_uid)
                return;

            bool exists = buff_comp.buffList.Any(buff_info => buff_info.buffData.id == buff_id);
            if (exists)
                return;
            
            BdPackage.BdUtils.log($"--- CardEffect_AddShield --- card[{card_comp}] --- selected_cards.Count: {GameController.instance.selected_cards.Count}, use_target_num: {card_comp.card_data.use_target_num}");

            var buff_data = ScriptableObject.CreateInstance<BuffData>();
            
            buff_data.id = buff_id;
            buff_data.className = GetType().ToString();
            buff_data.buffName = "圣盾";
            buff_data.description = $"每回合免疫第一次受到的伤害";
            buff_data.isForever = true; 
            buff_data.maxStack = 1;
            
            var add_buff_module = new BuffEffect_Shield();
            // buff_data.OnCreate = add_buff_module;
            add_buff_module.card_comp = card_comp;
            
            buff_data.OnCreate = add_buff_module;
            buff_data.OnEventCallBack = add_buff_module;
            
            // var be_hurt_buff_module = new BuffEffect_Shield();
            // be_hurt_buff_module.card_comp = card_comp;
            buff_data.OnBeHurt = add_buff_module;
            
            var buffInfo = new BuffInfo();
            buffInfo.buffData = buff_data;
            buffInfo.curStack = 1;
            buffInfo.target = card;
            buffInfo.creator = card;
            
            buff_comp.AddBuff(buffInfo);
            
            // var event_i = EventManager.instance.add_event(eventType: GameEventTypeEnum.OnManualRefreshStore.ToString(), removeWhenRunOnce: false,
            //     event_callback: (sender, args) => {
            //         // add_buff_module.Apply(buffInfo);
            //         BdPackage.BdUtils.log("--- on CardEffect_AddShield event_callback!");
            //         add_buff_module.reset_is_first_be_hurt();
            //         return "";
            //     });
            //
            // buff_comp.add_event_listener(event_i);
            
            var eventName = $"shield__uid[{creator_card_uid}]__card_name[{card_comp.card_data.card_name}]__{GetType().Name}";
            // BdPackage.BdUtils.log($"========= eventName 0: {eventName}, bool: {BdPackage.BdUtils.conv_to_bool(eventName)}");
            
            if (card_comp.parameter.card_uid != creator_card_uid)
                return;
            
            // var event_i = EventManager.instance.add_event(
            //     eventType: GameEventTypeEnum.OnRefreshStore.ToString(),
            //     removeWhenRunOnce: false,
            //     eventName: eventName
            //     );
            
            var event_i = new AppEventUtils.OrderedEventBase(
                eventType: GameEventTypeEnum.OnRefreshStore.ToString(),
                eventName: eventName,
                removeWhenRunOnce: false
            );

            event_i.EventData = card;
            
            event_i.event_callback = (sender, args) =>
            {
                var current_card = (GameObject)event_i.EventData;
                var new_card_comp = current_card.GetComponent<CardController>();
                BdPackage.BdUtils.log("*** shield OnAppearInStore --- reset_is_first_be_hurt! card_name:", card_comp.card_data.card_name, ", new_card_name:", new_card_comp.card_data.card_name);
                // add_buff_module.reset_is_first_be_hurt();
                buff_data.OnEventCallBack.Apply(buffInfo);
                return "";
            };
            event_i.EventName = eventName;
            
            EventManager.instance.add_event(event_i);
            buff_comp.add_event_listener(event_i);
        }
    }
}

